package me.wangxuan.pat.view;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractCellEditor;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;
import javax.swing.table.TableCellEditor;

public class TwoButtonsEditor extends AbstractCellEditor implements TableCellEditor {
	private static final long serialVersionUID = 1L;
	private JPanel panel;
	private JButton button1;
	private JButton button2;
	private int row;

	public TwoButtonsEditor(JTable table, ActionListener listener1, ActionListener listener2) {
		panel = new JPanel();
		panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS)); // 水平布局
		panel.setBorder(new LineBorder(UIManager.getColor("Table.gridColor"), 1)); // 添加网格线
//        panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5)); // 设置水平间距为5像素
//        panel.setBorder(new LineBorder(table.getGridColor(), 1)); // 添加网格线
		button1 = new JButton("生成");
		button2 = new JButton("删除");
		// 设置按钮大小
//        button1.setMaximumSize(new Dimension(40, 20));
//        button2.setMaximumSize(new Dimension(40, 20));

		button1.addActionListener(listener1);
		button2.addActionListener(listener2);
		// 添加左边的填充
		panel.add(Box.createHorizontalGlue());
		panel.add(button1);
		panel.add(Box.createHorizontalStrut(10)); // 10 像素的间隔
		panel.add(button2);
		// 添加右边的填充
		panel.add(Box.createHorizontalGlue());
	}

	@Override
	public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
		this.row = row;
		button1.putClientProperty("row", row);
		button2.putClientProperty("row", row);
		return panel;
	}

	@Override
	public Object getCellEditorValue() {
		return ""; // 返回一个空字符串，因为我们不需要保存任何值
	}
}
